/*
 * Copyright 2020-2021 redragon.dongbin
 *
 * This file is part of redragon-erp/赤龙ERP.

 * redragon-erp/赤龙ERP is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.

 * redragon-erp/赤龙ERP is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with redragon-erp/赤龙ERP.  If not, see <https://www.gnu.org/licenses/>.
 */
package com.erp.ledger.balance.dao.hibernate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;
import org.redragon.framework.annotation.Cache;
import org.redragon.framework.annotation.Permissions;
import org.redragon.framework.annotation.Permissions.PermissionType;
import org.redragon.framework.annotation.SqlParam;
import org.redragon.framework.hibernate.DaoSupport;
import org.redragon.framework.hibernate.model.Pages;
import org.redragon.framework.util.DaoCRUDUtil;
import com.erp.ledger.balance.dao.GLBalanceDao;
import com.erp.ledger.balance.dao.model.GLBalance;
import com.erp.ledger.balance.dao.model.GLBalanceCO;
import com.erp.masterdata.expense.dao.model.MdExpenseItem;

@Repository
public class GLBalanceDaoImpl implements GLBalanceDao{ 

    //注入DaoSupport工具类
    @Autowired
    private DaoSupport daoSupport;
    
    @Override
    public void insertDataObject(GLBalance obj) {
        this.daoSupport.insertDataTransaction(obj);
    }

    @Override
    public void updateDataObject(GLBalance obj) {
        this.daoSupport.updateDataTransaction(obj);
    }
    
    @Override
    public void insertOrUpdateDataObject(GLBalance obj) {
        this.daoSupport.insertOrUpdateDataTransaction(obj);
    }

    @Override
    public void deleteDataObject(GLBalance obj) {
        this.daoSupport.deleteDataTransactionJPA(obj);
    }

    @Override
    public List<GLBalance> getDataObjects() {
        return this.daoSupport.getDataAllObject(GLBalance.class);
    }

    @Override
    public GLBalance getDataObject(int id) {
        return (GLBalance)this.daoSupport.getDataObject(GLBalance.class, id);
    }
    
    @Override
    public GLBalance getDataObject(String code) {
        return null;
    }
    
    @Override
    public List<GLBalance> getDataObjects(GLBalanceCO paramObj) {
        String sql = "select p.* from gl_balance p where 1=1";
        
        Map<String, Object> args = new HashMap<String, Object>();
        sql = sql + DaoCRUDUtil.getSQLSearch(paramObj, "status", "and p.", args);
        sql = sql + DaoCRUDUtil.getSQLSearch(paramObj, "actualFlag", "and p.", args);
        sql = sql + " order by p.balance_id";
        
        Map<String, Class<?>> entity = new HashMap<String, Class<?>>();
        entity.put("p", GLBalance.class);
        
        return this.daoSupport.selectDataSql(sql, entity, args);
    }
    
    @Override
    public List<GLBalance> getDataObjects(Pages pages) {
        return null;
    }
    
    @Override
    public List<GLBalance> getDataObjects(Pages pages, GLBalanceCO paramObj) {
        String sql = "select p.* from gl_balance p where 1=1";
        
        Map<String, Object> args = new HashMap<String, Object>();
        sql = sql + DaoCRUDUtil.getSQLSearch(paramObj, "ledgerCode", "and p.", args);
        sql = sql + DaoCRUDUtil.getSQLSearch(paramObj, "subjectCode", "and p.", args);
        sql = sql + DaoCRUDUtil.getSQLSearch(paramObj, "currencyCode", "and p.", args);
        sql = sql + DaoCRUDUtil.getSQLSearch(paramObj, "periodName", "and p.", args);
        sql = sql + DaoCRUDUtil.getSQLSearch(paramObj, "actualFlag", "and p.", args);
        sql = sql + DaoCRUDUtil.getSQLSearch(paramObj, "status", "and p.", args);
        sql = sql + " order by p.balance_id";
        
        this.daoSupport.validateDataObject(paramObj);
        
        Map<String, Class<?>> entity = new HashMap<String, Class<?>>();
        entity.put("p", GLBalance.class);
        
        return this.daoSupport.getDataSqlByPage(sql, entity, args, pages);
    }

    @Override
    public List<Map<String, Object>> getDataObjectsArray(Pages pages, GLBalanceCO paramObj) {
        return null;
    }
    
    @Override
    @Permissions(PermissionType.DATA_AUTH)
    public List<GLBalance> getDataObjectsForDataAuth(@SqlParam String dataAuthSQL, Pages pages, GLBalanceCO paramObj) {
        return null;
    }
    
    @Override
    public int getGLBalanceNumForActual(String ledgerCode) {
        String sql = "select 1 from gl_balance b where b.actual_flag = 'Y' and b.ledger_code = :ledgerCode limit 0,1";
        
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("ledgerCode", ledgerCode);
        
        List list = this.daoSupport.selectDataSqlCount(sql, args);
        if(list!=null&&list.size()>0) {
            return 1;
        }
        
        return 0;
    }
    
    @Override
    public boolean isExistBeginBalanceForSubject(String ledgerCode, String subjectCode) {
        String sql = "select count(*) from gl_balance b where b.actual_flag = 'N' and b.ledger_code = :ledgerCode and b.subject_code = :subjectCode";
        
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("ledgerCode", ledgerCode);
        args.put("subjectCode", subjectCode);
        
        List list = this.daoSupport.selectDataSqlCount(sql, args);
        if(list!=null&&list.size()>0) {
            int count = this.daoSupport.convertSQLCount(list.get(0));
            if(count>0) {
                return true;
            }
        }
        
        return false;
    }
    
    @Override
    public void updateBeginBalanceStatus(String ledgerCode, String status) {
        String sql = "update gl_balance set status = :status where actual_flag = 'N'";
        
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("status", status);
        
        this.daoSupport.executeSQLTransaction(sql, args);
    }
    
    @Override
    public GLBalance getLastGLBalanceBySubject(String ledgerCode, String subjectCode) {
        String sql = "select b.* from gl_balance b "
                   + "where b.ledger_code = :ledgerCode and b.subject_code = :subjectCode and b.status = 'Y' "
                   + "order by balance_id desc limit 0,1";
        
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("ledgerCode", ledgerCode);
        args.put("subjectCode", subjectCode);
        
        Map<String, Class<?>> entity = new HashMap<String, Class<?>>();
        entity.put("b", GLBalance.class);
        
        List list = this.daoSupport.selectDataSql(sql, entity, args);
        if(list!=null&&list.size()>0) {
            return (GLBalance)list.get(0);
        }
        
        return null;
    }
    
    @Override
    public List<GLBalance> getGLBalanceListBySubject(String ledgerCode, String periodName, String subjectCode) {
        String sql = "select b.* from gl_balance b "
                + "where b.ledger_code = :ledgerCode and b.period_name = :periodName and b.subject_code = :subjectCode "
                + "order by balance_id";
     
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("ledgerCode", ledgerCode);
        args.put("periodName", periodName);
        args.put("subjectCode", subjectCode);
         
        Map<String, Class<?>> entity = new HashMap<String, Class<?>>();
        entity.put("b", GLBalance.class);
         
        return this.daoSupport.selectDataSql(sql, entity, args);
    }
    
}
